import { messages } from "share/common";
import React, { Component } from "react";
import { Button, Form, Spin, Checkbox, Affix } from "antd";
import AirServer from "../airline-server";
import { message } from "antd/lib/index";

const FormItem = Form.Item;

@Form.create()
class SetNotice extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      settingData: {},
    };
  }

  componentWillMount() {
    this.initData();
  }

  initData = () => {
    AirServer(this)
      .getAirModelData({ companyOid: this.props.companyOID })
      .then((res) => {
        this.setState({ settingData: res.data.body }, this.props.form.resetFields);
      });
  };

  onSaveSetting = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const { settingData } = this.state;
        const params = Object.assign({}, settingData, values);
        params.companyOid = this.props.companyOID;
        AirServer(this)
          .saveAirModelData(params)
          .then((res) => {
            if (res.data.success) {
              this.initData();
              message.success(messages("supplier-1.key355") /*保存成功*/);
            } else {
              message.error(res.data.message);
            }
          });
      }
    });
  };

  render() {
    const { loading, settingData } = this.state;
    const { getFieldDecorator } = this.props.form;
    return (
      <div className="airline-config-container">
        <Spin className="spin" spinning={loading}>
          <Form>
            <h2>{messages("supplier-1.key377") /*短信通知*/}</h2>
            <a className="a-line-bottom">
              {
                messages(
                  "supplier-1.key378"
                ) /*汇联易与机票服务商都会给你的员工发送短信，若不需要，可以取消汇联易的短信消息通知*/
              }
            </a>
            <FormItem>
              {getFieldDecorator("enabledSmsNotice", {
                initialValue: settingData.enabledSmsNotice || false,
                valuePropName: "checked",
              })(<Checkbox>{messages("supplier-1.key35") /*启用temp*/}</Checkbox>)}
            </FormItem>
          </Form>
          <br />
          <br />
          <br />
          <br />
          <br />
          <br />
        </Spin>
        <Affix offsetBottom={0} className="setting-fotter-class">
          <Button style={{ marginLeft: 20 }} type="primary" onClick={this.onSaveSetting}>
            {messages("supplier-1.key356") /* 保存配置*/}
          </Button>
        </Affix>
      </div>
    );
  }
}

export default SetNotice;
